package cmsi.Controller;

import cmsi.vo.CurrentUserModel;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import net.lcheng.model.User;
import net.lcheng.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zwr on 2016/8/31.
 */
@Controller
public class MyController {
    @Autowired
    private UserService userService;
    /**
     * 获取个人资料
     *
     */
    @RequestMapping(value = "/my", method = RequestMethod.GET)
    public String dataGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser.isAuthenticated()) {
            //获取当前用户
            Session session = currentUser.getSession();
            CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            User user = userService.getUserByUserId(currentUserModel.getUserId());
            model.addAttribute("user", user);
            }else{
            return "redirect:/login";
        }
        return "my";

        }

    //退出
    @RequestMapping(value = "/sign", method = RequestMethod.GET)
    public String logout(HttpServletRequest request) {
        String redirectUrl = request.getParameter("rec");
        SecurityUtils.getSubject().logout();
        if (StringUtils.isEmpty(redirectUrl)) {
            return "redirect:/login";
        } else {
            return "redirect:" + redirectUrl;
        }
    }
}
